package com.example.rabbitmq.controller.ttl;

import com.example.rabbitmq.Constants;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/producer")
public class TTLProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/ttl")
    public String ttl() {
        CorrelationData correlationData = new CorrelationData("4");
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,"ttl",
                "ttl test...",message -> {
            message.getMessageProperties().setExpiration("10000");
            return message;});
        return "发送消息成功";
    }

    @RequestMapping("/ttl2")
    public String ttl2() {
        CorrelationData correlationData = new CorrelationData("5");
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE2,"ttl",
                "ttl2 test...",correlationData);
        return "发送消息成功";
    }
}
